//package cn.ilikes.tools.nosql.jpa.clients.indexs.index.lucenes;
//
//import java.io.IOException;
//
//import org.apache.lucene.document.Document;
//import org.apache.lucene.index.CorruptIndexException;
//import org.apache.lucene.index.DirectoryReader;
//import org.apache.lucene.index.IndexReader;
//import org.apache.lucene.index.IndexWriter;
//import org.apache.lucene.index.IndexWriterConfig;
//import org.apache.lucene.index.Term;
//import org.apache.lucene.search.IndexSearcher;
//import org.apache.lucene.store.Directory;
//import org.apache.lucene.util.Version;
//import org.slf4j.Logger;
//
//import cn.ilikes.tools.logs.Loggerfactory;
//import cn.ilikes.tools.nosql.jpa.clients.indexs.ClientIndexingRunTimeException;
//import cn.ilikes.tools.nosql.jpa.clients.indexs.IClientIndex;
//import cn.ilikes.tools.nosql.jpa.clients.indexs.index.ABIndexSource;
//import cn.ilikes.tools.nosql.jpa.clients.indexs.index.ABLuceneIndexSource;
//import cn.ilikes.tools.nosql.jpa.clients.indexs.units.LuceneQueryUtils;
//import cn.ilikes.tools.nosql.jpa.metadatas.EntityMetaData;
//
//public class SyncLuceneClientIndex extends LuceneClientIndex {
//	private Logger logger = Loggerfactory.instance(SyncLuceneClientIndex.class);
//
//	private IndexWriter indexWriter;
//
//	private IndexSearcher indexSearcher;
//
//	private IndexReader indexReader;
//
//	public  SyncLuceneClientIndex(ABIndexSource indexSource) {
//		super(indexSource);
//
//	}
//
//	@Override
//	protected IndexWriter createIndexWriter() {
//		if (indexWriter == null) {
//			synchronized (logger) {
//				try {
//					ABLuceneIndexSource luceneIndexSource = getIndexSource();
//					if (Loggerfactory.isdebug(logger))
//						Loggerfactory.info(logger, "create  " + luceneIndexSource.getIndexConfigs().getName() + "   index writer ");
//					Directory directory = luceneIndexSource.createDirectory();
//					IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_41, this.getAnalyzer()));
//					this.indexWriter = indexWriter;
//				} catch (Exception e) {
//					Loggerfactory.error(logger, e.getMessage(), e);
//
//				}
//			}
//		}
//		return indexWriter;
//	}
//
//	@Override
//	protected IndexSearcher createIndexSearcher() {
//		if (indexSearcher == null) {
//			synchronized (logger) {
//				try {
//					ABLuceneIndexSource luceneIndexSource = getIndexSource();
//					if (Loggerfactory.isdebug(logger))
//						Loggerfactory.info(logger, "create  " + luceneIndexSource.getIndexConfigs().getName() + "   index read  " + this.indexWriter);
//					IndexReader reader = createIndexReader();
//					if (Loggerfactory.isdebug(logger))
//						Loggerfactory.info(logger, "create  " + luceneIndexSource.getIndexConfigs().getName() + "  index searcher " + reader);
//					IndexSearcher searcher = new IndexSearcher(reader);
//					this.indexSearcher = searcher;
//
//				} catch (Exception e) {
//					Loggerfactory.error(logger, e.getMessage(), e);
//				}
//			}
//		}
//		return indexSearcher;
//	}
//
//	@Override
//	protected IndexReader createIndexReader() {
//		if (indexReader == null) {
//			synchronized (logger) {
//				try {
//
//					ABLuceneIndexSource luceneIndexSource = getIndexSource();
//					if (Loggerfactory.isdebug(logger))
//						Loggerfactory.info(logger, "create  " + luceneIndexSource.getIndexConfigs().getName() + "  index reader  " + this.indexReader);
//					//					Directory directory = luceneIndexSource.createDirectory();
//					IndexWriter indexWriter = createIndexWriter();
//					IndexReader reader = DirectoryReader.open(indexWriter, true);
//					this.indexReader = reader;
//				} catch (Exception e) {
//					Loggerfactory.error(logger, e.getMessage());
//				}
//			}
//		}
//		return indexReader;
//	}
//
//	@Override
//	protected void indexDocument(EntityMetaData metadata, Document document, Object entity, IndexAction indexAction) {
//
//		if (Loggerfactory.isdebug(logger))
//			Loggerfactory.debug(logger, "Indexing document: " + document + " for in file system using Lucene");
//		IndexWriter indexWriter = createIndexWriter();
//		try {
//			switch (indexAction) {
//			case insert:
//				if (Loggerfactory.isdebug(logger))
//					Loggerfactory.debug(logger, "Lucene indexing insert : " + entity.toString());
//				indexWriter.addDocument(document);
//				break;
//			case update: {
//				Term trem = new Term(IClientIndex.JPA_SQL_ID_FIELD, LuceneQueryUtils.getJPAHBaseKeyofEntity(metadata, entity));
//				if (Loggerfactory.isdebug(logger))
//					Loggerfactory.debug(logger, "Lucene indexing update  : " + LuceneQueryUtils.getJPAHBaseKeyofEntity(metadata, entity));
//				indexWriter.updateDocument(trem, document);
//			}
//				break;
//
//			default:
//				break;
//			}
//
//		} catch (CorruptIndexException e) {
//			Loggerfactory.error(logger, "Error while indexing document " + document + " into Lucene. Details:" + e.getMessage());
//			throw new ClientIndexingRunTimeException("Error while indexing document " + document + " into Lucene.", e);
//		} catch (IOException e) {
//			Loggerfactory.error(logger, "Error while indexing document " + document + " into Lucene. Details:" + e.getMessage());
//			e.printStackTrace();
//			throw new ClientIndexingRunTimeException("Error while indexing document " + document + " into Lucene.", e);
//		}
//	}
//
//	@Override
//	protected void removeDocument(Term term, IndexAction indexAction) {
//		IndexWriter indexWriter = createIndexWriter();
//		try {
//			switch (indexAction) {
//			case remove:
//				if (Loggerfactory.isdebug(logger))
//					Loggerfactory.debug(logger, "Lucene indexing reomve  : " + term.toString());
//				indexWriter.deleteDocuments(term);
//				break;
//			default:
//				break;
//			}
//
//		} catch (CorruptIndexException e) {
//			Loggerfactory.error(logger, "Error while remove  document  into Lucene. Details : " + e.getMessage());
//			throw new ClientIndexingRunTimeException("Error while remove  document  into Lucene. Details : " + e.getMessage(), e);
//		} catch (IOException e) {
//			Loggerfactory.error(logger, "Error while remove  document  into Lucene. Details : " + e.getMessage());
//			e.printStackTrace();
//			throw new ClientIndexingRunTimeException("Error while remove  document  into Lucene. Details : " + e.getMessage(), e);
//		}
//		
//
//	}
//
//}
